package com.yanzhenjie.andserver.slice;

import com.yanzhenjie.andserver.MyCommonEventSubscriber;
import com.yanzhenjie.andserver.hos.ResourceTable;
import com.yanzhenjie.andserver.util.ToastUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.event.commonevent.CommonEventManager;
import ohos.event.commonevent.CommonEventSubscribeInfo;
import ohos.event.commonevent.MatchingSkills;
import ohos.rpc.RemoteException;
import ohos.utils.net.Uri;
import org.apache.commons.lang3.StringUtils;

public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    private String action = "com.yanzhenjie.andserver.hos";
    private MyCommonEventSubscriber myCommonEventSubscriber;
    private Intent mService;
    private Text mDesc;
    private Button mStart,mStop,mBrowser;
    private String host;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        initView();

        onRegisterCommonEventSubscriber();
    }

    /**
     * 初始化 view
     */
    private void initView(){
        mStart = (Button)findComponentById(ResourceTable.Id_start_server);
        mStop = (Button)findComponentById(ResourceTable.Id_stop_server);
        mBrowser = (Button)findComponentById(ResourceTable.Id_open_browser);
        mDesc = (Text)findComponentById(ResourceTable.Id_server_desc);
        mStart.setClickedListener(this);
        mStop.setClickedListener(this);
        mBrowser.setClickedListener(this);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()){
            case ResourceTable.Id_start_server:
                Operation operation = new Intent.OperationBuilder()
                        .withBundleName(getBundleName())
                        .withAbilityName("com.yanzhenjie.andserver.ServiceAbility")
                        .build();
                mService = new Intent();
                mService.setOperation(operation);
                startAbility(mService);
                break;
            case ResourceTable.Id_stop_server:
                if(mService == null){
                    ToastUtil.showShort(this,"服务未启动");
                    return;
                }
                stopAbility(mService);
                mService = null;
                host = null;
                break;
            case ResourceTable.Id_open_browser:
                if(StringUtils.isEmpty(host) || host.length() == 0){
                    ToastUtil.showShort(this,"服务未启动，请先开启服务");
                    return;
                }
                Intent intent = new Intent();
                intent.setAction("android.intent.action.VIEW");
                Uri uri = Uri.parse(host);
                intent.setUri(uri);
                startAbility(intent);
                break;
        }
    }

    /**
     * 订阅无序的公共事件
     */
    private void onRegisterCommonEventSubscriber(){
        MatchingSkills matchingSkills = new MatchingSkills();
        matchingSkills.addEvent(action); // 自定义事件
        CommonEventSubscribeInfo subscribeInfo = new CommonEventSubscribeInfo(matchingSkills);
        myCommonEventSubscriber = new MyCommonEventSubscriber(subscribeInfo,this);
        try {
            CommonEventManager.subscribeCommonEvent(myCommonEventSubscriber);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    /**
     * 开启服务
     * @param ip
     */
    public void onStartServer(String ip){
        this.host = "http://"+ip+":8080/";
        mDesc.setText("服务已启动，地址为：http://"+ip+":8080/");
    }

    /**
     * 关闭服务
     */
    public void onStopServer(){
        mDesc.setText("服务已停止");
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }


}
